package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.Registration;
import com.woniu.smallmedical.entity.ResponseResult;
import com.woniu.smallmedical.mapper.BasicmessageMapper;
import com.woniu.smallmedical.mapper.PrescriptionDetailsMapper;
import com.woniu.smallmedical.mapper.PrescriptionMapper;
import com.woniu.smallmedical.mapper.RegistrationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author author
 * @create 2021-10-08-15:21
 */
@RestController
public class PrescriptionController {
    @Autowired
    BasicmessageMapper basicmessageMapper;

    @Autowired
    PrescriptionMapper prescriptionMapper;

    @Autowired
    PrescriptionDetailsMapper prescriptionDetailsMapper;

    @Autowired
    RegistrationMapper registrationMapper;


    @GetMapping("getBasicmessageByName")
    public ResponseResult getBasicmessageByName(String name) {
        return new ResponseResult(basicmessageMapper.queryByName(name), 200, "按照名称查询药品信息");
    }


    @GetMapping("savePrescription")
    public ResponseResult savePrescription(String diagnosis, String doctorsorder, String total, String numberdays, String bmid, String inid, String paid) {

        // 处方表id
        Integer prid = Integer.valueOf(generateUID());

        // 处方详细表id
        Integer pdid = Integer.valueOf(generateUID());

        // 药品信息表id
        Integer bmidInt = Integer.valueOf(bmid);

        // 库存表id
        Integer inidInt = Integer.valueOf(inid);

        // 总量
        Integer totalInt = Integer.valueOf(total);

        // 天数
        Integer numberdaysInt = Integer.valueOf(numberdays);

        // 查询挂号id
        Registration bySickid = registrationMapper.getBySickid(Integer.valueOf(paid));

        // 医嘱，诊断
        prescriptionMapper.insert(prid, diagnosis, doctorsorder, bySickid.getRid());
        prescriptionDetailsMapper.insert(pdid, prid, bmidInt, inidInt, totalInt, numberdaysInt);
        return new ResponseResult(null, 200, "保存处方");
    }

    public String generateUID() {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < 8; i++) {
            //首字母不能为0
            result += (random.nextInt(9) + 1);
        }
        return result;
    }

}
